<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Permission_entity{
	var $current_dbprefix = "";
	var $id = 0;
	var $name = '';
	var $slug = '';
	
	function __construct() {
		$ci= & get_instance();
		$this->current_dbprefix = $ci->db->dbprefix;
		$ci->db->set_dbprefix('default_');
	}
	function __destruct() {
		$ci= & get_instance();
		$ci->db->set_dbprefix($this->current_dbprefix);
	}
	
	function setId($input){
		$this->id = $input;
	}
	function setName($input){
		$this->name = $input;
	}
	
	function setSlug($input){
		$this->slug = $input;
	}
	
	function getId(){
		return $this->id;
	}
	function getName(){
		return $this->name;
	}
	function getSlug(){
		return $this->slug;
	}
	
	function init($cmd, $input)
	{
		$ci=& get_instance();
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_PERMISSION)->result();
		}
		else if($cmd == 'name')
		{
			$result = $ci->db->where('name',$input)->get(TBL_PERMISSION)->result();
		}
		else if($cmd == 'slug')
		{
			$result = $ci->db->where('slug',$input)->get(TBL_PERMISSION)->result();
		}else
		{
			return false;
		}
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->name = $result[0]->name;
			$this->slug = $result[0]->slug;
			return $this;
		}
		return false;
	}
	function getPermissionList(){
		$ci=& get_instance();
		return $ci->db->get(TBL_PERMISSION)->result();
	}
	function save($entity){
		$ci=& get_instance();
		$ci->db->set('name',$entity->getName());
		$ci->db->set('slug',$entity->getSlug());
		$ci->db->insert(TBL_PERMISSION);
		return $ci->db->insert_id();
	}
	function update(){
		$ci=& get_instance();
		$ci->db->set('name', $this->name);
		$ci->db->set('slug', $this->slug);
		return $ci->db->where('id', $this->id)->update(TBL_PERMISSION);
	}
	function delete($id){
		$ci=& get_instance();
		$ci->db->where('id',$id);
		return $ci->db->delete(TBL_PERMISSION);
	}
	function checkExistedName($id, $name){
		$ci=& get_instance();
		if($id){
			$ci->db->where('id !=',$id);
		}
		$ci->db->where('name',$name);
		return $ci->db->get(TBL_PERMISSION)->result();
	}
}